package cn.com.jtang.wedding.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import cn.com.jtang.wedding.model.AdminUser;
import cn.com.jtang.wedding.service.AdminUserManager;
import cn.com.jtang.wedding.util.EncryptUtil;
import cn.com.jtang.wedding.util.NumberUtil;

import com.opensymphony.xwork2.Preparable;

public class AdminUserAction extends BaseAction implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private AdminUserManager adminUserManager;
	private String adminaccount;
	private String password;
	
	private String resetPass;
	private Long pid;

	// 修改密码时用到的两次密码输入
	private String password1, password2;

	private AdminUser adminUser;

	private List<AdminUser> adminUserList;

	private List<Long> ids;

	private Long id;
	
	public void prepare() throws Exception {

	}

	public String doAdminLogin() {

		if (adminaccount == null || password == null) {
			return ERROR;
		}
		AdminUser user = adminUserManager.queryByAdminAccount(adminaccount);
		if (user == null) {
			return ERROR;
		}
		if (user.getPassword().equals(EncryptUtil.Encrypt(password))
				&& user.getAdminType() == 1L) {
			HttpSession session = getSession();
			session.putValue("currentadminuser", user);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String doAgentLogin() {

		if (adminaccount == null || password == null) {
			return ERROR;
		}
		AdminUser user = adminUserManager.queryByAdminAccount(adminaccount);
		if (user == null) {
			return ERROR;
		}
		if (user.getPassword().equals(EncryptUtil.Encrypt(password))
				&& user.getAdminType() == 2L) {
			HttpSession session = getSession();
			session.putValue("currentadminuser", user);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String doChannelLogin() {

		if (adminaccount == null || password == null) {
			return ERROR;
		}
		AdminUser user = adminUserManager.queryByAdminAccount(adminaccount);
		if (user == null) {
			return ERROR;
		}
		if (user.getPassword().equals(EncryptUtil.Encrypt(password))
				&& user.getAdminType() == 3L) {
			HttpSession session = getSession();
			session.putValue("currentadminuser", user);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String toEditAdminUser() {
		adminUser = getCurrentAdminUser();
		return SUCCESS;
	}

	// 修改公司的基本信息，除密码、ID等重要信息外
	public String editAdminUser() {
		// 防止用户恶意修改重要信息
		AdminUser originalUser = getCurrentAdminUser();
		AdminUser a=adminUserManager.queryByAdminAccount(adminUser.getAdminUserName());
		if(a!=null&&(!a.getId().equals(originalUser.getId()))){
			return "existAdminUserSelf";
		}
		adminUser.setId(originalUser.getId());
		adminUser.setPassword(originalUser.getPassword());
		adminUser.setRegistTime(originalUser.getRegistTime());
		adminUser.setParentId(originalUser.getParentId());
		adminUser.setTimes(originalUser.getTimes());
		adminUser.setAdminType(originalUser.getAdminType());

		if (adminUserManager.update(adminUser) > 0) {
			getSession().putValue("currentadminuser", adminUser);
			return SUCCESS;
		} else {
			return "fail";
		}

	}

	// 修改密码
	public String editPassword() {
		if (password == null
				|| !(EncryptUtil.Encrypt(password.trim())
						.equals(getCurrentAdminUser().getPassword()))) {
			return "passwordError";
		}
		if (password1 == null || !(password1.trim().equals(password2))) {
			return "modifyPassError";
		}

		password1 = EncryptUtil.Encrypt(password1);
		adminUser = getCurrentAdminUser();
		adminUser.setPassword(password1);
		if (adminUserManager.update(adminUser) > 0) {
			return SUCCESS;
		} else {
			return "modifyPassError";
		}
	}

	private Long adminType; // 必填 管理员类型 1管理员 2总代 3渠道

	/**
	 * 添加渠道
	 * 
	 * @return
	 */

	public String addChannel() {
		adminUser.setAdminType(Long.valueOf(3));
		adminUser.setTimes(Long.valueOf(0));
		return this.addAdminUser();
	}

	private String addAdminUser() {
		if(adminUserManager.queryByAdminAccount(adminUser.getAdminUserName())!=null){
			return "adminUserExist";
		}
		if (adminUser.getPassword() == null||adminUser.getPassword().equals("")
				|| (!adminUser.getPassword().trim().equals(password1))) {
			return "fail";
		}
		adminUser.setPassword(EncryptUtil.Encrypt(adminUser.getPassword()));
		adminUser.setRegistTime(new Date());
		adminUser.setParentId(getCurrentAdminUser().getId());
		adminUserManager.add(adminUser);
		return SUCCESS;
	}

	/**
	 * 查看子管理员用户
	 * 
	 * @return
	 */
	public String listAdminUser() {
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", getCurrentAdminUser().getId());

		int count = adminUserManager.getCount(map);
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount=pageCount==0?1:pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		adminUserList = adminUserManager.queryAll(map, pageSize * (pageNum - 1),
				pageSize * pageNum);

		return SUCCESS;
	}

	/**
	 * 删除子管理员用户
	 * 
	 * @return
	 */
	public String deleteAdminUser() {
		if (ids == null||ids.size()==0) {
			return "fail";
		}
		for (Long id : ids) {
			adminUser = adminUserManager.queryById(id);
			// 如果是系统管理员,可直接删, 如果是总代,只能删除自己的渠道
			if (getCurrentAdminUser().getAdminType().equals(Long.valueOf(1))
					|| adminUser.getParentId().equals(
							getCurrentAdminUser().getId())) {
				adminUserManager.delete(id);
			}
		}
		return SUCCESS;
	}

	/**
	 * 修改子管理员用户信息
	 * @return
	 */
	
	public String toEditSubAdminUser(){
		if(id==null||id.equals(Long.valueOf(0))){
			return "fail";
		}
		adminUser=adminUserManager.queryById(id);
		return SUCCESS;
	}
	
	public String editSubAdminUser(){
		if(adminUserManager.queryByAdminAccount(adminUser.getAdminUserName())!=null){
			return "adminUserExist";
		}
		AdminUser orignal= adminUserManager.queryById(adminUser.getId());
		if(!(getCurrentAdminUser().getAdminType().equals(Long.valueOf(1)))&&!(orignal.getParentId().equals(getCurrentAdminUser().getId()))){
			return "fail";
		}
		adminUser.setParentId(orignal.getParentId());
		adminUser.setPassword(orignal.getPassword());
		adminUser.setRegistTime(orignal.getRegistTime());
		adminUser.setTimes(orignal.getTimes());
		
		adminUserManager.update(adminUser);
		return SUCCESS;
	}
	
	public String resetTimes(){
		if(id==null||id.equals(Long.valueOf(0))){
			return "fail";
		}
		adminUser=adminUserManager.queryById(id);
		adminUser.setTimes(Long.valueOf(0));
		adminUserManager.update(adminUser);
		return SUCCESS;
	}
	/**
	 * 添加总代
	 * 
	 * @return
	 */

	public String addAgent() {
		adminUser.setAdminType(Long.valueOf(2));
		adminUser.setTimes(Long.valueOf(0));
		return this.addAdminUser();
	}
	
	/**
	 * 方法描述：密码找回
	 */
	public String resetAdminPass(){		
		if(pid==null||pid.equals(Long.valueOf(0))){
			return "resetAPError";
		}		
		resetPass=NumberUtil.getUniqueID().substring(0, 6);
		adminUser=adminUserManager.queryById(pid);
		adminUser.setPassword(EncryptUtil.Encrypt(resetPass));
		adminUserManager.update(adminUser);
		return SUCCESS;
	}
	
	public AdminUserManager getAdminUserManager() {
		return adminUserManager;
	}

	public void setAdminUserManager(AdminUserManager adminUserManager) {
		this.adminUserManager = adminUserManager;
	}

	public String getAdminaccount() {
		return adminaccount;
	}

	public void setAdminaccount(String adminaccount) {
		this.adminaccount = adminaccount;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public AdminUser getAdminUser() {
		return adminUser;
	}

	public void setAdminUser(AdminUser adminUser) {
		this.adminUser = adminUser;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}	

	public String getResetPass() {
		return resetPass;
	}

	public List<AdminUser> getAdminUserList() {
		return adminUserList;
	}

	public void setAdminUserList(List<AdminUser> adminUserList) {
		this.adminUserList = adminUserList;
	}

	public Long getAdminType() {
		return adminType;
	}

	public void setAdminType(Long adminType) {
		this.adminType = adminType;
	}

	public List<Long> getIds() {
		return ids;
	}

	public void setIds(List<Long> ids) {
		this.ids = ids;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}


	public void setResetPass(String resetPass) {
		this.resetPass = resetPass;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}
	
}
